package org.example;

public class SinlyLinkedList {
            private Node head=null;
            class Node {
                int value;
                Node next;
                 Node(int value, Node next) {
                     this.value = value;
                     this.next = next;
                 }
            }


   public void addFirst(int value) {

                head=new Node(value,head);
   }
   public void loop(){
                Node p=head;
                while(p!=null){
                    System.out.println(p.value);
                    p=p.next;
                }
   }
    public Node getlast() {
        Node p = head;
        if (p == null) return null;
        while (p.next != null) {
            p = p.next;
        }
        return p;
    }


    public void addLast(int value){
                Node last=getlast();
                if(last==null){
                    addFirst(value);
                    return;
                }
                last.next=new Node(value,null);
    }

//1113333
    public static void main(String[] args){
        SinlyLinkedList  list=new SinlyLinkedList();
//        list.addFirst(1);
//        list.addFirst(2);
//        list.addFirst(3);
//        list.addFirst(4);
        list.addLast(10);
        list.addLast(20);
        list.addLast(30);
        list.addLast(40);
        list.loop();
    }
}

